package com.tianji.promotion.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@Slf4j
public class PromotionConfig {

    @Bean
    public Executor generateExchangeCodeExecutor(){
        ThreadPoolTaskExecutor poolExecutor = new ThreadPoolTaskExecutor();
        // 1.核心线程池大小
        poolExecutor.setCorePoolSize(2);
        // 2.最大线程池大小
        poolExecutor.setMaxPoolSize(5);
        // 3.队列大小
        poolExecutor.setQueueCapacity(200);
        // 4.线程名称
        poolExecutor.setThreadNamePrefix("exchange-code-handler-");
        // 5.拒绝策略
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        poolExecutor.initialize();
        return poolExecutor;
    }
}
